package cn.rongdao.jrkabao.ui.message;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.RelativeLayout;

import com.chanven.lib.cptr.PtrClassicDefaultHeader;
import com.mifengkong.frtools.http.request.callback.FRBaseObserver;
import com.mifengkong.frtools.ui.activity.FRStartActivity;
import com.mifengkong.frwidget.recyclerview.PtrRecyclerViewUIComponent;
import com.mifengkong.frwidget.refersh.OnPullToRefreshListener;
import com.mifengkong.frwidget.titlebar.TitleBarUIComponent;

import java.util.ArrayList;
import java.util.List;

import cn.rongdao.jrkabao.R;
import cn.rongdao.jrkabao.base.BaseAutoFinishActivity;
import cn.rongdao.jrkabao.bean.message.LoanMessageBean;
import cn.rongdao.jrkabao.constant.InfoConstant;
import cn.rongdao.jrkabao.model.message.LoanMessageModel;
import cn.rongdao.jrkabao.ui.message.adapter.LoanMessageRecyclerViewAdapter;

/**
 * Created by jiangyongxing on 2016/11/17.
 * 描述：我的消息 界面
 */

public class LoanMessageActivity extends BaseAutoFinishActivity {

    private LoanMessageRecyclerViewAdapter adapter;
    private List<LoanMessageBean> dataEntityList = new ArrayList<>();
    private LinearLayoutManager linearLayoutManager;
    private RelativeLayout mEmptyMessage;
    private TitleBarUIComponent titleBarUIComponent;
    private PtrRecyclerViewUIComponent mPtr;

    public static void start(Context context) {
        FRStartActivity.start(context, LoanMessageActivity.class);
    }

    @Override
    protected int layoutRes() {
        return R.layout.activity_mymessage;
    }


    @Override
    protected void initView() {
        titleBarUIComponent = (TitleBarUIComponent) findViewById(R.id.amm_titlebar);
        initTitleBar();

        mPtr = (PtrRecyclerViewUIComponent) findViewById(R.id.am_ptr);
        mPtr.setBackground(R.color.layout_bg);
        mPtr.delayRefresh();
        mPtr.setOnPullToRefreshListener(new OnPullToRefreshListener() {
            @Override
            public void onPullToRefresh() {
                getData();
            }
        });
        mPtr.setHeaderType(PtrClassicDefaultHeader.HEADER_TYPE_ANIM);
        linearLayoutManager = new LinearLayoutManager(this);
        mPtr.setLayoutManager(linearLayoutManager);
        adapter = new LoanMessageRecyclerViewAdapter(this, dataEntityList);
        mPtr.setAdapter(adapter);
        mEmptyMessage = (RelativeLayout) findViewById(R.id.activity_my_message_empty);
        adapter.setOnItemActionListener(new LoanMessageRecyclerViewAdapter.OnItemActionListener() {
            @Override
            public void onItemClickListener(View v, int pos) {
                Intent intent = new Intent(LoanMessageActivity.this, LoanMessageDetailActivity.class);
                intent.putExtra("time", dataEntityList.get(pos).getTime());
                intent.putExtra("message", dataEntityList.get(pos).getMessage());
                startActivityForResult(intent, InfoConstant.REQUEST_CODE_MESSAGEACTIVITY);
            }

            @Override
            public boolean onItemLongClickListener(View v, int pos) {
                return false;
            }
        });
    }

    private void getData() {
        LoanMessageModel.getInstance().execute(new FRBaseObserver<List<LoanMessageBean>>(this) {
            @Override
            protected void onBaseNext(List<LoanMessageBean> data) {
                runOnUiThread();
                dataEntityList.clear();
                dataEntityList.addAll(data == null ? new ArrayList<LoanMessageBean>() : data);
                adapter.notifyDataSetChanged();

                mPtr.setVisibility(View.VISIBLE);
                mEmptyMessage.setVisibility(View.INVISIBLE);
            }

            @Override
            protected void onBaseNextButDataAndMessageIsEmpty() {
                runOnUiThread();
                mPtr.setVisibility(View.GONE);
                mEmptyMessage.setVisibility(View.VISIBLE);
            }
        });
    }

    private void runOnUiThread() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                mPtr.refreshComplete();
            }
        });
    }


    private void initTitleBar() {
        titleBarUIComponent.initTitleBlack("我的消息");
        titleBarUIComponent.setBottomViewVisiable(true);
        titleBarUIComponent.initLeftBackBlack(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        titleBarUIComponent.setBackgroundColor(getResources().getColor(R.color.white));
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
//        if (requestCode == AppConstant.REQUEST_CODE_MESSAGEACTIVITY)
        if (requestCode == InfoConstant.REQUEST_CODE_MESSAGEACTIVITY && resultCode == InfoConstant.RESULT_CODE_MESSAGEDETAILACTIVITY){
            getData();
            finish();
        }
    }
}
